package zust.se.springpro.controller;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import zust.se.springpro.entity.Project;
import zust.se.springpro.service.Impl.ProjectServiceImpl;
import zust.se.springpro.service.ProjectServiceI;

import javax.annotation.Resource;
import javax.validation.Valid;

@RequestMapping("/project")
public class ProjectController {
    @Resource
    ProjectServiceI projectServiceImpl;

    @RequestMapping("to_addproject")
    public String to_AddProject()
    {
        return "AddProject";
    }
    @RequestMapping("/addproject")
    public ModelAndView AddProject(@Valid Project project, BindingResult bindingResult) {
        ModelAndView mv = new ModelAndView();
        if (bindingResult.hasErrors()) {
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            mv.addObject("info", bindingResult.getFieldError().getDefaultMessage());
            mv.setViewName("project");
        } else {
            int tag = projectServiceImpl.addproject(project);
            if (tag == 0) {
                mv.addObject("info", "创建失败");
                mv.setViewName("project");
            } else mv.setViewName("project");
        }
        return mv;
    }
}
